<?php

class Application_Model_ItemMapper extends App_Model_Mapper
{
    protected $dbTableName = 'Application_Model_DbTable_Item';

    /*
     *  Peuple l'objet avec :
     *  _ ses propriétés
     *  _ ses relations avec les commandes et les commandes clients
     *
     * @param $item Application_Model_Item
     */
    public function findItemOrdersByReference(Application_Model_Item $item){

        $db = $this->getDbTable();
        $result = $db->fetchAll(array('reference = ?' => $item->getReference()));
        $row = $result->current();
        
        if($row instanceof Zend_Db_Table_Row_Abstract){
            $item->setId($row->id_art_ref);

            $rowItemOrders = $row->findDependentRowset('Application_Model_DbTable_ItemOrder');

            if($rowItemOrders->count() > 0){

                foreach($rowItemOrders as $rowItemOrder){

                    $itemOrder = new Application_Model_ItemOrder(array(
                        'id'        => $rowItemOrder->id_panier,
                        'quantity'  => $rowItemOrder->quantite,
                        'price'      => $rowItemOrder->prix_article
                    ));                    

                    $rowOrders = $rowItemOrder->findDependentRowset('Application_Model_DbTable_Order');

                    if(1 == $rowOrders->count()){
                        $order = new Application_Model_Order(array(
                                'id'    => $rowOrders[0]->id_panier_envoi
                        ));                        

                        $rowCustomerOrder = $rowOrders[0]->findDependentRowset('Application_Model_DbTable_CustomerOrder');
                        if(1 == $rowCustomerOrder->count()){
                            
                            $customerOrder = new Application_Model_CustomerOrder(array(
                                'id'            => $rowCustomerOrder[0]->id_panier,
                                'createdAt'     => $rowCustomerOrder[0]->date_enreg,
                                'sentAt'        => $rowCustomerOrder[0]->date_envoi,
                                'invoice'       => $rowCustomerOrder[0]->num_facture,
                                'refused'       => $rowCustomerOrder[0]->id_refusee
                            ));
                            $order->setCustomerOrder($customerOrder);
                            $itemOrder->setOrder($order);
                            $item->addItemOrder($itemOrder);
                        }                        
                    }                    
                }
            }       
        }
    }


    /*
     * Peuple l'objet avec :
     * _ ses propriétés
     * _ ses relations avec les avoirs client
     */
    public function findItemCreditNotesByReference(Application_Model_Item $item)
    {
        $db = $this->getDbTable();
        $result = $db->fetchAll(array('reference = ?' => $item->getReference()));
        $row = $result->current();

        if($row instanceof Zend_Db_Table_Row_Abstract){
            $item->setId($row->id_art_ref);
            $rowItemCreditNotes = $row->findDependentRowset('Application_Model_DbTable_ItemCreditNote');
            
            if($rowItemCreditNotes->count() > 0){
                foreach($rowItemCreditNotes as $rowItemCreditNote){
                    $itemCreditNote = new Application_Model_ItemCreditNote(array(
                        'id'        => $rowItemCreditNote->id_art_avoir,
                        'total'     => $rowItemCreditNote->montant_part
                    ));

                    $rowCreditNote = $rowItemCreditNote->findDependentRowset('Application_Model_DbTable_CreditNote');
                    if($rowCreditNote->count() > 0){
                        $creditNote = new Application_Model_CreditNote(array(
                            'id'        => $rowCreditNote->id_avoir,
                            'createdAt' => $rowCreditNote->date,
                            'total'     => $rowCreditNote->montant_total
                        ));
                        $itemCreditNote->setCreditNote($creditNote);
                        $item->addItemCreditNote($itemCreditNote);
                    }
                }
            }
        }
    }
}

